package com.wl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/s1")
public class SessionServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.通过请求创建Session
        HttpSession session = req.getSession(true);
        // 设置session的有效期(秒)
        // session.setMaxInactiveInterval(1800);

        // 2.输出客户拿到id
        System.out.println("手牌:" + session.getId());
        // 3.新客老客
        System.out.println("新客:" + session.isNew());

        // 4.如果是新的客户，把JSESSION的时效改成一天
        if (session.isNew()){
            Cookie c = new Cookie("JSESSIONID", session.getId());
            c.setMaxAge(60*60*24);
            resp.addCookie(c);
        }
    }
}
